<template>
    <el-dialog title="新建角色" v-model="EditMadelShow" width="500px">
        <div>
            <el-form :model="form" ref="ruleFormRef" :rules="rules" label-width="100px" size="large">
                <el-form-item label="角色名" prop="role">
                    <el-input v-model="form.role" maxlength="20" show-word-limit />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input v-model="form.remark" type="textarea" :rows="4" resize="none" maxlength="100"
                        show-word-limit />
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="EditMadelShow = false">取 消</el-button>
                <el-button type="primary" @click="handleSave(ruleFormRef)">确 定</el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref, computed } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'

// 获取父组件传过来的数据
let data = defineProps(['value'])

let emits = defineEmits(['update:value'])

// 控制弹窗显示隐藏
let EditMadelShow: any = computed({
    get() {
        return data.value
    },
    set(newVal) {
        emits("update:value", newVal);
    }
})

const ruleFormRef = ref<FormInstance>()

// 表单
let form = reactive({
    role: '',
    remark: ''
})

// 表单验证
const rules = reactive<FormRules>({
    role: [
        { required: true, message: '请输入角色名', trigger: 'blur' },
        { min: 2, max: 20, message: '账号的长度在2到20个字符之间', trigger: 'blur' },
    ]
})

// 保存数据
const handleSave = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid, fields) => {
        if (valid) {
            console.log(11111)
        } else {
            ElMessage.error('请正确填写表单')
        }
    })
}

</script>

<style lang="scss" scoped>
:deep .el-dialog__body {
    padding-bottom: 0 !important;
}
</style>